home *** CD-ROM | disk | FTP | other *** search
/ mail.altrad.com / 2015.02.mail.altrad.com.tar / mail.altrad.com / TEST / office deutch / INFOPATH.NL-NL / INFLR.CAB / TRAVREQ.XSN_1043 / script.js < prev    next >
Text File  |  2006-11-12  |  24KB  |  748 lines

  1. /*
  2.  * This file contains functions for data validation and form-level events.
  3.  * Because the functions are referenced in the form definition (.xsf) file, 
  4.  * it is recommended that you do not modify the name of the function,
  5.  * or the name and number of arguments.
  6.  *
  7. */
  8.  
  9. // The following line is created by Microsoft Office InfoPath to define the
  10. // prefixes for all the known namespaces in the main XML data file.
  11. // Any modification to the form files made outside of InfoPath
  12. // will not be autmatically updated.
  13. //<namespacesDefinition>
  14. XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD" xmlns:tr="http://schemas.microsoft.com/office/infopath/2003/sample/TravelRequest"');
  15. //</namespacesDefinition>
  16.  
  17. /*------------------------------------------------------------------------------
  18.     This function handler is generated and managed automatically.
  19.     Do not rename the function or alter its parameter list.
  20. ------------------------------------------------------------------------------*/
  21. function XDocument::OnLoad(oEvent)
  22. {
  23.     // Avoid DOM updates when the document has been digitally signed.
  24.     if (XDocument.IsSigned)
  25.         return;
  26.  
  27.     initializeNodeValue("/tr:travelRequest/tr:date", getDateString());
  28. }
  29.  
  30. /*------------------------------------------------------------------------------
  31.     This function handler is generated and managed automatically.
  32.     Do not rename the function or alter its parameter list.
  33. ------------------------------------------------------------------------------*/
  34. function msoxd__date::OnValidate(oEvent)
  35. {
  36.     // Get all the nodes we care about
  37.     var xmlData  = XDocument.DOM;
  38.     var xmlTripType = xmlData.selectSingleNode("/tr:travelRequest/tr:itinerary/tr:tripType");
  39.     var xmlReturnDate = xmlData.selectSingleNode("/tr:travelRequest/tr:itinerary/tr:returnDate");
  40.     var xmlReturnTime = xmlData.selectSingleNode("/tr:travelRequest/tr:itinerary/tr:returnTime");
  41.     var xmlAllLegs = xmlData.selectNodes("/tr:travelRequest/tr:itinerary/tr:itineraryLeg");
  42.  
  43.     // Loop through all itinerary legs.
  44.     var xmlLeg = xmlAllLegs.nextNode();
  45.     
  46.     // Don't validate when DOM is missing all legs
  47.     if (xmlLeg == null)
  48.         return;
  49.             
  50.     var xmlPrevDate = xmlLeg.selectSingleNode("tr:flight/tr:departureDate");
  51.     var xmlPrevTime = xmlLeg.selectSingleNode("tr:flight/tr:departureTime");
  52.     
  53.     while (xmlLeg = xmlAllLegs.nextNode())
  54.     {
  55.         var xmlThisDate = xmlLeg.selectSingleNode("tr:flight/tr:departureDate");
  56.         var xmlThisTime = xmlLeg.selectSingleNode("tr:flight/tr:departureTime");
  57.  
  58.         var L_FlightScheduling_ERR = "Deze reis moet plaatsvinden na de vorige reis.";
  59.         validateDateTime(oEvent, xmlPrevDate, xmlPrevTime, xmlThisDate, xmlThisTime, L_FlightScheduling_ERR, L_FlightScheduling_ERR);
  60.         
  61.         xmlPrevDate = xmlThisDate;
  62.         xmlPrevTime = xmlThisTime;
  63.     }
  64.  
  65.     var L_roundtrip_TXT = "retour";
  66.     if (xmlTripType.text == L_roundtrip_TXT)
  67.     {
  68.         var L_ReturnTime_ERR = "De tijd van terugkomst moet op of na de laatste vertrektijd vallen.";
  69.         var L_ReturnDate_ERR = "De datum van terugkomst moet op of na de laatste vertrekdatum vallen.";
  70.         validateDateTime(oEvent, xmlPrevDate, xmlPrevTime, xmlReturnDate, xmlReturnTime, L_ReturnDate_ERR, L_ReturnTime_ERR);
  71.     }
  72. }
  73.  
  74. /*------------------------------------------------------------------------------
  75.     This function handler is generated and managed automatically.
  76.     Do not rename the function or alter its parameter list.
  77. ------------------------------------------------------------------------------*/
  78. function msoxd__tripType::OnAfterChange(oEvent)
  79. {
  80.     // Avoid side effects when DOM is read only
  81.     if (oEvent.IsUndoRedo)
  82.         return;
  83.  
  84.     // Avoid DOM updates when the document has been digitally signed.
  85.     if (XDocument.IsSigned)
  86.         return;
  87.         
  88.     // Get all the nodes we care about
  89.     var xmlData  = XDocument.DOM;
  90.     var xmlTripType = xmlData.selectSingleNode("/tr:travelRequest/tr:itinerary/tr:tripType");
  91.     var xmlReturnDate = xmlData.selectSingleNode("/tr:travelRequest/tr:itinerary/tr:returnDate");
  92.  
  93.     // Only roundtrip has a return date.
  94.     var L_roundtrip_TXT = "retour";
  95.     if (getNodeValue(xmlTripType) != L_roundtrip_TXT)
  96.         setNil(xmlReturnDate);
  97. }
  98.  
  99. /*==============================================================================
  100.     Private functions
  101. ==============================================================================*/
  102.  
  103. /*------------------------------------------------------------------------------
  104.     Validate the date fields.
  105. ------------------------------------------------------------------------------*/
  106. function validateDateTime(oEvent, xmlPrevDate, xmlPrevTime, xmlThisDate, xmlThisTime, strDateError, strTimeError)
  107. {
  108.     // Ignore invalid or empty elements.
  109.     if (isInvalidOrEmpty(xmlPrevDate) || isInvalidOrEmpty(xmlThisDate))
  110.         return;
  111.         
  112.     // Valid XML values of type date and time could be compared lexicographically.
  113.     
  114.     // Most common case first - the dates are ordered correctly.
  115.     if (xmlPrevDate.text < xmlThisDate.text)
  116.         return;
  117.     
  118.     // Error - the dates are reversed.
  119.     if (xmlPrevDate.text > xmlThisDate.text)
  120.     {
  121.         oEvent.ReportError(xmlThisDate, strDateError, true);
  122.         return;
  123.     }
  124.     
  125.     // The dates are equal; the times should be ordered.
  126.     if (isInvalidOrEmpty(xmlPrevTime) || isInvalidOrEmpty(xmlThisTime))
  127.         return;    
  128.         
  129.     if (xmlPrevTime.text >= xmlThisTime.text)
  130.         oEvent.ReportError(xmlThisTime, strTimeError, true);
  131. }
  132.  
  133. /*------------------------------------------------------------------------------
  134.  
  135.     Common form template functions section - the following code is common to all out-of-box form templates.
  136.  
  137. ------------------------------------------------------------------------------*/
  138.  
  139. /* =============================================================================
  140.     Currency handling
  141. ============================================================================= */
  142.  
  143. /*------------------------------------------------------------------------------
  144.     updateCurrency()
  145. ------------------------------------------------------------------------------*/
  146. function updateCurrency(xmlNode, strPrefix)
  147. {
  148.     var xmlName = xmlNode.selectSingleNode(strPrefix + 'name');
  149.     var xmlSymbol = xmlNode.selectSingleNode(strPrefix + 'symbol');
  150.  
  151.     var strName = xmlName.text;
  152.     var strSymbol = lookupCurrencySymbol(strName);
  153.  
  154.     if (strSymbol)
  155.     {
  156.         setNodeValue(xmlSymbol, '(' + strSymbol + ') ');
  157.     }
  158.     else
  159.     {
  160.         setNodeValue(xmlName, '');
  161.         setNodeValue(xmlSymbol, '');
  162.     }
  163. }
  164.  
  165. /*------------------------------------------------------------------------------
  166.     lookupCurrencySymbol()
  167. ------------------------------------------------------------------------------*/
  168. function lookupCurrencySymbol(strName)
  169. {
  170.     var xmlCurDom = XDocument.GetDOM("currencies");
  171.     var xmlCurrency = xmlCurDom.selectSingleNode("/currencies/currency[@name = '" + strName + "']");
  172.  
  173.     if (xmlCurrency)
  174.         return xmlCurrency.getAttribute("symbol");
  175. }
  176.  
  177. /* =============================================================================
  178.     Date/Time handling
  179. ============================================================================= */
  180.  
  181. /*------------------------------------------------------------------------------
  182.     getDateString()
  183. ------------------------------------------------------------------------------*/
  184. function getDateString(oDate)
  185. {
  186.     // Use today as default value.
  187.     if (oDate == null)
  188.         oDate = new Date();
  189.  
  190.     var m = oDate.getMonth() + 1;
  191.     var d = oDate.getDate();
  192.  
  193.     if (m < 10)
  194.         m = "0" + m;
  195.  
  196.     if (d < 10)
  197.         d = "0" + d;
  198.  
  199.     // ISO 8601 date (YYYY-MM-DD).
  200.     return oDate.getFullYear() + "-" + m + "-" + d;
  201. }
  202.  
  203. /*------------------------------------------------------------------------------
  204.     getTimeString()
  205. ------------------------------------------------------------------------------*/
  206. function getTimeString(oTime)
  207. {
  208.     // Use now as default value.
  209.     if (oTime == null)
  210.         oTime = new Date();
  211.  
  212.     var h = oTime.getHours();
  213.     var m = oTime.getMinutes();
  214.     var s = oTime.getSeconds();
  215.     var ms = oTime.getMilliseconds();
  216.  
  217.     if (h < 10)
  218.         h = "0" + h;
  219.  
  220.     if (m < 10)
  221.         m = "0" + m;
  222.  
  223.     if (s < 10)
  224.         s = "0" + s;
  225.  
  226.     if (ms < 100)
  227.         ms = "0" + (ms < 10 ? "0" : "") + ms;
  228.  
  229.     // ISO 8601 time (HH:MM:SS.SSS).
  230.     return h + ":" + m + ":" + s + "." + ms;
  231. }
  232. /* =============================================================================
  233.     Language conversion operations    
  234. ============================================================================= */
  235.  
  236. /*------------------------------------------------------------------------------
  237.     convertXMLNumberToJScript()
  238. ------------------------------------------------------------------------------*/
  239. function convertXMLNumberToJScript(value)
  240. {
  241.     var retVal;
  242.  
  243.     switch (value)
  244.     {
  245.         case "-INF":
  246.             retVal = Number.NEGATIVE_INFINITY;
  247.             break;
  248.  
  249.         case "INF":
  250.             retVal = Number.POSITIVE_INFINITY;
  251.             break;
  252.  
  253.         case "NaN":
  254.             retVal = NaN;
  255.             break;
  256.  
  257.         default:
  258.             //If no prefix of its argument can be parsed as a
  259.             //float, parseFloat will return NaN. Otherwise, it
  260.             //will return the value of the longest float prefix
  261.             //it can find in its argument.
  262.             retVal = parseFloat(value);
  263.     }
  264.  
  265.     return retVal;
  266. }
  267.  
  268. /*------------------------------------------------------------------------------
  269.     convertJScriptNumberToXML()
  270. ------------------------------------------------------------------------------*/
  271. function convertJScriptNumberToXML(value)
  272. {
  273.     var retVal;
  274.  
  275.     switch (value)
  276.     {
  277.         case Number.NEGATIVE_INFINITY:
  278.             retVal = "-INF";
  279.             break;
  280.  
  281.         case Number.POSITIVE_INFINITY:
  282.             retVal = "INF";
  283.             break;
  284.  
  285.         //NaN == NaN returns false. Any other value is equal to itself,
  286.         //which is why the case below differentiates between NaN and
  287.         //any other valid value
  288.         case value:
  289.             retVal = value;
  290.             break;
  291.  
  292.         default:
  293.             retVal = "NaN";
  294.     }
  295.  
  296.     return retVal;
  297. }
  298.  
  299. /* =============================================================================
  300.     Node value operations
  301. ============================================================================= */
  302.  
  303. /*------------------------------------------------------------------------------
  304.     isInvalidOrEmpty()
  305. ------------------------------------------------------------------------------*/
  306. function isInvalidOrEmpty(xmlNode)
  307. {
  308.     // If there is no value, ignore it.
  309.     if (!xmlNode || !xmlNode.text)
  310.         return true;
  311.  
  312.     // The caller can pass additional error types as optional arguments.
  313.     var aErrorTypes = new Array;
  314.     if (arguments.length > 1)
  315.     {
  316.         for (var i=1; i<arguments.length; i++)
  317.             aErrorTypes.push(arguments[i]);
  318.     }
  319.     else
  320.     {
  321.         aErrorTypes.push("SCHEMA_VALIDATION");
  322.     }
  323.  
  324.     // If there is a validation error related to this node,
  325.     // then the node is invalid.
  326.     for (var i=0; i<XDocument.Errors.Count; i++)
  327.     {
  328.         var oError = XDocument.Errors(i);
  329.  
  330.         if (xmlNode == oError.Node)
  331.         {
  332.             for (var j in aErrorTypes)
  333.             {
  334.                 if (oError.Type == aErrorTypes[j])
  335.                     return true;
  336.             }
  337.         }
  338.     }
  339.  
  340.     // Is valid (no error was found).
  341.     return false;
  342. }
  343.  
  344. /*------------------------------------------------------------------------------
  345.     getNodeValue()
  346. ------------------------------------------------------------------------------*/
  347. function getNodeValue(xpath, defaultValue)
  348. {
  349.     var xmlNode = getNode(xpath);
  350.  
  351.     if (isInvalidOrEmpty(xmlNode))
  352.         return (arguments.length > 1) ? defaultValue : "";
  353.     else
  354.         return xmlNode.text;
  355. }
  356.  
  357. /*------------------------------------------------------------------------------
  358.     getNodeTypedValue()    - Use instead of getNodeValue() if you want
  359.         to parse the value by MSXML according to data type (from schema).
  360.         
  361.     NOTE: this is not really useful for dates in Jscript.
  362. ------------------------------------------------------------------------------*/
  363. function getNodeTypedValue(xpath, defaultValue, fNumber)
  364. {
  365.     var xmlNode = getNode(xpath);
  366.  
  367.     if (isInvalidOrEmpty(xmlNode))
  368.         return (arguments.length > 1) ? defaultValue : "";
  369.     
  370.     if (fNumber)
  371.         return convertXMLNumberToJScript(xmlNode.nodeTypedValue);
  372.     else
  373.         return xmlNode.nodeTypedValue;
  374. }
  375.  
  376. /*------------------------------------------------------------------------------
  377.     setNodeValue()
  378. ------------------------------------------------------------------------------*/
  379. function setNodeValue(xpath, value)
  380. {
  381.     var xmlNode = getNode(xpath);
  382.  
  383.     if (!xmlNode)
  384.         return;
  385.  
  386.     // The xsi:nil needs to be removed before we set the value.
  387.     if (value != "" && xmlNode.getAttribute("xsi:nil"))
  388.         xmlNode.removeAttribute("xsi:nil");
  389.  
  390.     // Setting the value would mark the document as dirty.
  391.     // Let's do that if the value has really changed.
  392.     if (xmlNode.text != value)
  393.         xmlNode.text = value;
  394. }
  395.  
  396. /*------------------------------------------------------------------------------
  397.     setNodeTypedValue() - Use instead of setNodeValue() if you want
  398.         to format the value by MSXML according to data type (from schema).
  399.         
  400.         Use this when setting floating point and decimal values. These
  401.         values are formatted differently in some locales and will cause schema
  402.         validation errors when set using setNodeValue or a node's text property.
  403.         
  404.     NOTE: this is not really useful for dates in Jscript.
  405. ------------------------------------------------------------------------------*/
  406. function setNodeTypedValue(xpath, value)
  407. {
  408.     var xmlNode = getNode(xpath);
  409.     
  410.     if (!xmlNode)
  411.         return;
  412.  
  413.     // The xsi:nil needs to be removed before we set the value.
  414.     if (value != "" && xmlNode.getAttribute("xsi:nil"))
  415.         xmlNode.removeAttribute("xsi:nil");
  416.  
  417.     var convertedValue = convertJScriptNumberToXML(value);
  418.  
  419.     // Setting the value would mark the document as dirty.
  420.     // Let's do that if the value has really changed.
  421.     if (xmlNode.nodeTypedValue != convertedValue)
  422.         xmlNode.nodeTypedValue = convertedValue;
  423. }
  424.  
  425. /*------------------------------------------------------------------------------
  426.     setRoundedValue() - Rounds the value to the default number of decimal
  427.     places and calls setNodeTypedValue() with the result.
  428. ------------------------------------------------------------------------------*/
  429. function setRoundedValue(xpath, value)
  430. {
  431.     var xmlNode = getNode(xpath);
  432.  
  433.     if (!xmlNode)
  434.         return;
  435.  
  436.     var roundedValue = roundFloat(value, 3);
  437.     setNodeTypedValue(xpath, roundedValue);
  438. }
  439.  
  440. /*------------------------------------------------------------------------------
  441.     setNil() - Empty a nillable element.
  442. ------------------------------------------------------------------------------*/
  443. function setNil(xpath)
  444. {
  445.     var xmlNode = getNode(xpath);
  446.  
  447.     if (!xmlNode || xmlNode.text === "")
  448.         return;
  449.  
  450.     // Create xsi:nil attribute with the proper namespace.
  451.     var xmlNil = xmlNode.ownerDocument.createNode(2, "xsi:nil", "http://www.w3.org/2001/XMLSchema-instance");
  452.     xmlNil.text = "true";
  453.  
  454.     // The order is important.
  455.     xmlNode.text = "";
  456.     xmlNode.setAttributeNode(xmlNil);
  457. }
  458.  
  459. /*------------------------------------------------------------------------------
  460.     roundFloat() - Rounds the value to the specified number of decimal places.
  461. ------------------------------------------------------------------------------*/
  462. function roundFloat(value, decimalPlaces)
  463. {
  464.     if (value < -1E15 || 1E15 < value)
  465.     {
  466.         return value;
  467.     }
  468.     else
  469.     {
  470.         var nPowerToRound = Math.pow(10, decimalPlaces);
  471.         return Math.round(value*nPowerToRound)/nPowerToRound;
  472.     }
  473. }
  474.  
  475. /*------------------------------------------------------------------------------
  476.     normalizeSource() - Ensures that we always get a node of type NODE_ELEMENT
  477.         or NODE_ATTRIBUTE.
  478. ------------------------------------------------------------------------------*/
  479. function normalizeSource(oEvent)
  480. {
  481.     // Return the node's parent if the node is of type NODE_TEXT and its parent
  482.     // is of type NODE_ELEMENT
  483.     if (oEvent.Parent.nodeType == 1 && oEvent.Source.nodeType == 3)
  484.         return oEvent.Parent;
  485.         
  486.     // Return the node if it is of type NODE_ELEMENT or NODE_ATTRIBUTE;
  487.     if (oEvent.Source.nodeType == 1 || oEvent.Source.nodeType == 2)
  488.         return oEvent.Source;
  489. }
  490.  
  491. /*------------------------------------------------------------------------------
  492.     normalizeParent() - Ensures that we always get the parent of a node of 
  493.         type NODE_ELEMENT or NODE_ATTRIBUTE.
  494. ------------------------------------------------------------------------------*/
  495. function normalizeParent(oEvent)
  496. {
  497.     // Return the node if it is of type NODE_ELEMENT or NODE_ATTRIBUTE;
  498.     if (oEvent.Source.nodeType == 1 || oEvent.Source.nodeType == 2)
  499.         return oEvent.Parent;
  500.         
  501.     // Return the node 's parent if the node is of type NODE_TEXT and its parent
  502.     // is of type NODE_ELEMENT
  503.     if (oEvent.Parent.nodeType == 1 && oEvent.Source.nodeType == 3)
  504.         return oEvent.Parent.parentNode;
  505. }
  506.  
  507. /*------------------------------------------------------------------------------
  508.     initializeNodeValue()
  509. ------------------------------------------------------------------------------*/
  510. function initializeNodeValue(xpath, strValue)
  511. {
  512.     var xmlNode = getNode(xpath);
  513.  
  514.     // Set the node value *ONLY* if the node is empty.
  515.     if (xmlNode.text == "")
  516.         setNodeValue(xmlNode, strValue);
  517. }
  518.  
  519. /* =============================================================================
  520.     Simple nodelist operations
  521. ============================================================================= */
  522.  
  523. /*------------------------------------------------------------------------------
  524.     getNode()
  525. ------------------------------------------------------------------------------*/
  526. function getNode(xpath)
  527. {
  528.     // Both XML node and absolute XPath are allowed.
  529.     if (typeof(xpath) == "string")
  530.         return XDocument.DOM.selectSingleNode(xpath);
  531.     else
  532.         return xpath;
  533. }
  534.  
  535. /*------------------------------------------------------------------------------
  536.     getNodeList()
  537. ------------------------------------------------------------------------------*/
  538. function getNodeList(xpath)
  539. {
  540.     // Both XML node and absolute XPath are allowed.
  541.     if (typeof(xpath) == "string")
  542.         return XDocument.DOM.selectNodes(xpath);
  543.     else
  544.         return xpath;
  545. }
  546.  
  547. /*------------------------------------------------------------------------------
  548.     count()
  549. ------------------------------------------------------------------------------*/
  550. function count(xpath)
  551. {
  552.     var xmlNodeList = getNodeList(xpath);
  553.  
  554.     if (xmlNodeList)
  555.         return xmlNodeList.length;
  556.     else
  557.         return -1;
  558. }
  559.  
  560. /* =============================================================================
  561.     Complex nodelist operations
  562. ============================================================================= */
  563.  
  564. /*------------------------------------------------------------------------------
  565.     applyAction() - Iterate the whole nodelist and apply a given action
  566.         to each node in the nodelist. You can implement your own actions.
  567.  
  568.     Example:
  569.         var oAction = new Object();
  570.         oAction.sum = 0;
  571.         oAction.apply = new Function("xmlNode", "this.sum += xmlNode.nodeTypedValue");
  572. ------------------------------------------------------------------------------*/
  573. function applyAction(xpath, oAction)
  574. {
  575.     var xmlNodeList = getNodeList(xpath);
  576.     var xmlNode;
  577.  
  578.     while (xmlNode = xmlNodeList.nextNode())
  579.         oAction.apply(xmlNode);
  580.  
  581.     xmlNodeList.reset();
  582. }
  583.  
  584. /*------------------------------------------------------------------------------
  585.     sum()
  586. ------------------------------------------------------------------------------*/
  587. function sum(xpath)
  588. {
  589.     var oAction = new Object;
  590.     oAction.nResult = 0;
  591.     oAction.apply = new Function("xmlNode", "this.nResult += getNodeTypedValue(xmlNode, 0, true)");
  592.  
  593.     applyAction(xpath, oAction);
  594.     return oAction.nResult;
  595. }
  596.  
  597. /*------------------------------------------------------------------------------
  598.     anyInvalidOrEmpty() - Searches a nodelist to see if there are any invalid 
  599.         or empty nodes.
  600. ------------------------------------------------------------------------------*/
  601. function anyInvalidOrEmpty(xpath)
  602. {
  603.     var oAction = new Object;
  604.     oAction.fInvalid = false;
  605.     oAction.apply = new Function("xmlNode", "this.fInvalid |= isInvalidOrEmpty(xmlNode)");
  606.     
  607.     applyAction(xpath, oAction);
  608.     return oAction.fInvalid;
  609. }
  610.  
  611. /*------------------------------------------------------------------------------
  612.     reindex()
  613. ------------------------------------------------------------------------------*/
  614. function reindex(xpath, iStart)
  615. {
  616.     if (iStart == null)
  617.         iStart = 1;
  618.  
  619.     var oAction = new Object;
  620.     oAction.i = iStart;
  621.     oAction.apply = new Function("xmlNode", "setNodeTypedValue(xmlNode, this.i++)");
  622.  
  623.     return applyAction(xpath, oAction);
  624. }
  625.  
  626. /*------------------------------------------------------------------------------
  627.     average()
  628. ------------------------------------------------------------------------------*/
  629. function average(xpath, xpathWeights)
  630. {
  631.     var oAction = new Object;
  632.     oAction.nSum = 0;
  633.     oAction.cNodes = 0;
  634.  
  635.     if (!xpathWeights)
  636.     {
  637.         oAction.apply = actionSimpleAverage;
  638.         oAction.result = resultSimpleAverage;
  639.     }
  640.     else
  641.     {
  642.         oAction.nWeights = 0;
  643.         oAction.xmlWeights = getNodeList(xpathWeights);
  644.         oAction.apply = actionWeightedAverage;
  645.         oAction.result = resultWeightedAverage;
  646.     }
  647.  
  648.     applyAction(xpath, oAction);
  649.     return oAction.result();
  650. }
  651.  
  652. /*------------------------------------------------------------------------------
  653.     actionSimpleAverage()
  654. ------------------------------------------------------------------------------*/
  655. function actionSimpleAverage(xmlNode)
  656. {
  657.     if (!isInvalidOrEmpty(xmlNode))
  658.     {
  659.         this.nSum += getNodeTypedValue(xmlNode, 0, true);
  660.         this.cNodes++;
  661.     }
  662. }
  663.  
  664. /*------------------------------------------------------------------------------
  665.     resultSimpleAverage()
  666. ------------------------------------------------------------------------------*/
  667. function resultSimpleAverage()
  668. {
  669.     if (this.cNodes)
  670.         return this.nSum / this.cNodes;
  671.     else
  672.         return 0;
  673. }
  674.  
  675. /*------------------------------------------------------------------------------
  676.     actionWeightedAverage()
  677. ------------------------------------------------------------------------------*/
  678. function actionWeightedAverage(xmlNode)
  679. {
  680.     // There should be one weight for each node. Even if the node is invalid,
  681.     // we have to advance the iterator of weights to keep them in sync.
  682.     var xmlWeight = this.xmlWeights.nextNode();
  683.  
  684.     if (!isInvalidOrEmpty(xmlNode) && !isInvalidOrEmpty(xmlWeight))
  685.     {
  686.         var nValue = getNodeTypedValue(xmlNode, 0, true);
  687.         var nWeight = getNodeTypedValue(xmlWeight, 1, true);
  688.  
  689.         this.nSum += nValue * nWeight;
  690.         this.nWeights += nWeight;
  691.         this.cNodes++;
  692.     }
  693. }
  694.  
  695. /*------------------------------------------------------------------------------
  696.     resultWeightedAverage()
  697. ------------------------------------------------------------------------------*/
  698. function resultWeightedAverage()
  699. {
  700.     this.xmlWeights.reset();
  701.  
  702.     if (this.cNodes)
  703.     {
  704.         if (this.nWeights > 0)
  705.             return this.nSum / this.nWeights;
  706.         else
  707.             return this.nSum
  708.     }
  709.     else
  710.     {
  711.         return 0;
  712.     }
  713. }
  714.  
  715. /*------------------------------------------------------------------------------
  716.     sort()
  717. ------------------------------------------------------------------------------*/
  718. function sort(xpathParent, xpathChildren, fnCompare)
  719. {
  720.     // Retrieve the parent node; all the children will be sorted.
  721.     var xmlOrigParent = getNode(xpathParent)
  722.     var xmlItems = xmlOrigParent.selectNodes(xpathChildren);
  723.  
  724.     if (1 < count(xmlItems))
  725.     {
  726.         // Store the (pointers to) items in an array for faster access.
  727.         rgItems = new Array();
  728.         
  729.         while (xmlItem = xmlItems.nextNode())
  730.             rgItems.push(xmlItem);
  731.             
  732.         // Sort the array (the XML does not change).
  733.         rgItems.sort(fnCompare);
  734.  
  735.         // Now that the array is sorted the DOM should be updated.
  736.         var xmlSortParent = xmlOrigParent.cloneNode(true);
  737.         var xmlClones = xmlSortParent.selectNodes(xpathChildren);
  738.         xmlClones.removeAll();
  739.         
  740.         // Update the nodelist, each item is moved only once.
  741.         // (each change is causing several notifications to be fired.)
  742.         for (var i=0; i<rgItems.length; i++)
  743.             xmlSortParent.appendChild(rgItems[i].cloneNode(true));
  744.         
  745.         xmlOrigParent.parentNode.replaceChild(xmlSortParent, xmlOrigParent);
  746.     }
  747. }
  748.